package com.hongdee.crm.carConfig.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.carConfig.entity.TCrmCarRef;
import com.hongdee.crm.carConfig.service.ITCrmCarRefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/2/20.
 */
@RestController
@RequestMapping("/crm/config")
public class TCrmCarRefController {
    @Autowired
    private ITCrmCarRefService itCrmCarRefService;
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc
     * @author 徐钦勇
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmCarRef data) throws Exception {
        EntityUtils.valid(data);
        String oid = TokenUtil.getCompanyId(uid);
        data.setCompanyId(oid);
        TCrmCarRef group = this.itCrmCarRefService.saveOrUpdate(data);
        ResultEntity res = new ResultEntity();
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @param start 当前页面
     * @param limit 页面大小
     * @param group   分组
     * @return Map
     * @throws Exception
     * @desc
     * @author 徐钦勇
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "group",defaultValue = "") String group,
                    @RequestParam(name = "name", defaultValue = "")String name,
                    @RequestParam(name = "type", defaultValue = "")String type) throws  Exception{
        String oid=TokenUtil.getCompanyId(uid);
        Map map=this.itCrmCarRefService.page(start,limit,name,group,type,oid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc    启用start
     * @author 徐钦勇
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itCrmCarRefService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   停用stop
     * @author 徐钦勇
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itCrmCarRefService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }
    /**
     * @param  uid 登录用户id
     * @param  token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   车辆分配明细
     * @author 徐钦勇
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = this.itCrmCarRefService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
    /**
     * @param  uid 登录用户id
     * @param  token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   车辆分配删除
     * @author 徐钦勇
     */
    @RequestMapping("/del")
    @ResponseBody
    public ResultEntity del(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itCrmCarRefService.deleteById(id);
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @param type 角色类型 SA 保险接待 客服专员
     * @return Map
     * @throws Exception
     * @desc    人员下拉框
     * @author 徐钦勇
     */
    @RequestMapping("/userCombo")
    @ResponseBody
    public List<Map<String,Object>> userCombo(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "companyId", defaultValue = "") String companyId,
                    @RequestParam(name = "type", defaultValue = "0") String type)throws  Exception{
//        String oid=TokenUtil.getCompanyId(uid);
        return this.itCrmCarRefService.queryUserCombo(uid,type,companyId);
    }
    /**
     * 公司下所有的角色
     */
    @RequestMapping("/roleCombo")
    @ResponseBody
    public List<Map<String,Object>> roleCombo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                              @RequestParam(name = "token", defaultValue = "") String token)throws  Exception{
        String oid=TokenUtil.getCompanyId(uid);
        return this.itCrmCarRefService.roleCombo(uid);
    }
    /**
     * 公司下所有的用户
     */
    @RequestMapping("/userComBo")
    @ResponseBody
    public List<Map<String,Object>> userCombo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                              @RequestParam(name = "token", defaultValue = "") String token,
                                              @RequestParam(name = "companyId", defaultValue = "") String companyId)throws  Exception{
        if("".equals(companyId)) {
            companyId= TokenUtil.getCompanyId(uid);
        }
        return this.itCrmCarRefService.queryUserCombo1(uid,companyId);
    }

    /**
     * @Name: 部门下所有用户
     * @Author: Liu XiaoLong
     * @Date: 2018/3/9 17:14
     * @Remark:
     */
    @RequestMapping("/organUserCombo")
    @ResponseBody
    public List<Map<String,Object>> organUserCombo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                              @RequestParam(name = "token", defaultValue = "") String token,
                                              @RequestParam(name = "companyId", defaultValue = "") String companyId)throws  Exception{
        if("".equals(companyId)) {
            companyId= TokenUtil.getCompanyId(uid);
        }
        return this.itCrmCarRefService.queryOrganUserCombo(uid,companyId);
    }

}
